﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using CommonLib.Log;

namespace CommonLib.Log
{
    public static class StringExt
    {
        private static string logPath = ConfigurationManager.AppSettings["CommonLog"] == null ? string.Empty : ConfigurationManager.AppSettings["CommonLog"].ToString();

        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static string FormatWith(this string format, params object[] args)
        {
            string ret = string.Empty;

            try
            {
                ret = string.Format(format, args);
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "FormatWith => 将值[{0}]进行格式失败[{1}]".FormatWith(format, ex.Message), logPath);
            }

            return ret;
        }

        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }

        public static string Match(this string s, string pattern)
        {
            if (s == null) return "";
            return Regex.Match(s, pattern).Value;
        }

        public static bool IsInt(this string s)
        {
            int i;
            return int.TryParse(s, out i);
        }

        public static int ToInt(this string s)
        {
            int ret = -1;

            try
            {
                ret = int.Parse(s);
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "ToInt => 将值[{0}]转换为整形失败[{1}]".FormatWith(s, ex.Message), logPath);
            }
            return ret;
        }

        public static decimal ToDecimal(this string s)
        {
            decimal ret = -1;

            try
            {
                ret = decimal.Parse(s);
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "ToDecimal => 将值[{0}]转换为小数失败[{1}]".FormatWith(s, ex.Message), logPath);
            }
            return ret;
        }

        public static DateTime ToDateTime(this string s)
        {
            DateTime dt = DateTime.Now;

            try
            {
                dt = DateTime.Parse(s);
            }
            catch (Exception ex)
            {
                Logger.getInstance().WriteToLog((int)LogType.E, "ToDateTime => 将值[{0}]转换为日期失败[{1}]".FormatWith(s, ex.Message), logPath);
            }

            return dt;
        }

        public static string ToCamel(this string s)
        {
            if (IsNullOrEmpty(s)) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        public static string ToPascal(this string s)
        {
            if (IsNullOrEmpty(s)) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        public static string Left(this string s, int length)
        {
            string result = s.Substring(0, length);
            return result;
        }

        public static string Right(this string s, int length)
        {
            string result = s.Substring(s.Length - length, length);
            return result;
        }

        public static string Mid(this string s, int startIndex, int length)
        {
            string result = s.Substring(startIndex, length);
            return result;
        }

        public static string Mid(this string s, int startIndex)
        {
            string result = s.Substring(startIndex);
            return result;
        }

        public static string StringToJson(this string s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {

                char c = s[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':      //退格
                        sb.Append("\\b");
                        break;
                    case '\f':      //走纸换页
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n"); //换行    
                        break;
                    case '\r':      //回车
                        sb.Append("\\r");
                        break;
                    case '\t':      //横向跳格
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
            return sb.ToString();
        }

        public static string StringDanYinToJSON(this string ors)
        {
            ors = ors == null ? "" : ors;
            StringBuilder buffer = new StringBuilder(ors);
            int i = 0;
            while (i < buffer.Length)
            {
                if (buffer[i] == '\'' || buffer[i] == '\\')
                {
                    buffer.Insert(i, '\\');
                    i += 2;
                }
                else
                {
                    i++;
                }
            }
            return buffer.ToString();
        }
    }
}
